home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
WASTE 1.2
/
WASTE Demo ƒ
/
WEDemoEvents.c
< prev
next >
Wrap
Text File
|
1996-06-20
|
10KB
|
496 lines
/*
WASTE Demo Project:
Events Handling
Copyright © 1993-1996 Marco Piovanelli
All Rights Reserved
C port by John C. Daub
*/
#ifndef __APPLEEVENTS__
#include <AppleEvents.h>
#endif
#ifndef __AEREGISTRY__
#include <AERegistry.h>
#endif
#ifndef __DISKINIT__
#include <DiskInit.h>
#endif
#ifndef __TEXTSERVICES__
#include <TextServices.h>
#endif
#ifndef __WEDEMOAPP__
#include "WEDemoIntf.h"
#endif
static UInt32 sSleepTime = 0; // sleep time for WaitNextEvent()
static RgnHandle sMouseRgn = nil; // mouse region for WaitNextEvent()
void AdjustCursor( Point mouseLoc, RgnHandle mouseRgn )
{
WindowRef window;
// by default, set mouseRgn to the whole QuickDraw coordinate plane,
// so that we never get mouse moved events
SetRectRgn( mouseRgn, -SHRT_MAX, -SHRT_MAX, SHRT_MAX, SHRT_MAX );
// give text services a chance to set the cursor shape
if ( gHasTextServices )
{
if ( SetTSMCursor( mouseLoc ) )
return;
}
// if there is a window open, give WEAdjustCursor an opportunity to set the cursor
// WEAdjustCursor intersects mouseRgn (if supplied) with a region within which
// the cursor is to retain its shape
// (if the cursor is outside the view region, this is subtracted from mouseRgn )
if ( ( window = FrontWindow( ) ) != nil )
{
if ( WEAdjustCursor( mouseLoc, mouseRgn, GetWindowWE( window ) ) )
return;
}
// set the cursor to the arrow cursor
SetCursor( &qd.arrow );
}
void DoMouseDown( const EventRecord *event )
{
WindowRef window;
short partCode;
// find out where this click when down in
partCode = FindWindow( event->where, &window );
// dispatch on partCode
switch ( partCode )
{
case inMenuBar:
PrepareMenus( );
DoMenuChoice( MenuSelect( event->where ), event->modifiers );
break;
case inSysWindow:
#if defined ( UNIVERSAL_INTERFACES_VERSION ) && ( UNIVERSAL_INTERFACES_VERSION >= 0x211 )
SystemClick( event, window );
#else
// Universal Headers prior to version 2.1.2 define SystemClick
// incorrectly if STRICT_WINDOWS == 1
SystemClick( event, (WindowPtr) window );
#endif
break;
case inContent:
if ( DoContent( event->where, event, window ) )
{
SelectWindow( window );
}
break;
case inDrag:
DoDrag( event->where, window );
break;
case inGrow:
DoGrow( event->where, window );
break;
case inGoAway:
if ( TrackGoAway( window, event->where ) )
{
if ( DoClose( closingWindow, savingAsk, window ) != noErr )
{
// insert error handling
}
}
break;
case inZoomIn:
case inZoomOut:
if ( TrackBox( window, event->where, partCode ) )
{
DoZoom( partCode, window );
}
break;
} // switch
}
void DoKeyDown( const EventRecord *event )
{
short key;
Boolean isCmdKey;
// extract character code from event message
key = ( event->message & charCodeMask );
// is this a command+key combo?
isCmdKey = ( ( event->modifiers & cmdKey ) != 0 );
// map function keys to the equivalent command+key combos
// note that all fuction keys generate the same code, i.e. 0x10
if ( key == 0x10 )
{
isCmdKey = true;
switch( ( event->message & keyCodeMask ) >> 8 )
{
case keyF1:
key = 'z';
break;
case keyF2:
key = 'x';
break;
case keyF3:
key = 'c';
break;
case keyF4:
key = 'v';
break;
default:
key = 0;
} // switch
} // if
// command + printable character combos are routed to MenuKey( )
// but be sure to pass command + arrow key combos to WEKey( )
if ( isCmdKey && (key >= 0x20 ) )
{
PrepareMenus( );
DoMenuChoice( MenuKey( key ), event->modifiers );
}
else
{
DoKey( key, event );
}
}
void DoDiskEvent( const EventRecord *event )
{
Point dialogCorner;
if ( ( event->message >> 16) != noErr )
{
SetPt( &dialogCorner, 112, 80 );
DIBadMount( dialogCorner, event->message );
}
}
void DoOSEvent( const EventRecord *event )
{
short osMessage;
WindowRef window;
// extract the OS message field from the event record
osMessage = ( event->message & osEvtMessageMask) >> 24 ;
// dispatch on osMessage
switch( osMessage )
{
case suspendResumeMessage:
if ( ( window = FrontWindow( ) ) != nil )
{
DoActivate( (event->message & resumeFlag) != 0, window );
}
break;
case mouseMovedMessage:
// nothing
break;
}
}
void DoHighLevelEvent( const EventRecord *event )
{
AEProcessAppleEvent( event );
}
void DoNullEvent( const EventRecord *event )
{
#pragma unused (event)
WindowRef window;
if ( ( window = FrontWindow( ) ) != nil )
{
WEIdle( &sSleepTime, GetWindowWE(window) );
}
else
{
sSleepTime = LONG_MAX;
}
}
void DoWindowEvent( const EventRecord *event )
{
WindowRef window;
// the message field of the event record contains the window reference
window = (WindowRef) event->message;
// make sure this window is an application window; check the windowKind field
if ( GetWindowKind( window ) != userKind )
return;
switch ( event->what )
{
case updateEvt:
DoUpdate( window );
break;
case activateEvt:
DoActivate( ( event->modifiers & activeFlag) != 0, window );
break;
}
}
void ProcessEvent( void )
{
EventRecord event;
Boolean gotEvent;
gotEvent = WaitNextEvent( everyEvent, &event, sSleepTime, sMouseRgn );
// give text services a chance to intercept this event
// if TSMEvent( ) handles the event, it will set event.what to nullEvent
if ( gHasTextServices )
{
TSMEvent( &event );
}
// adjust cursor shape and set mouse region
// (we assume event.where is the current mouse position in global coordinates
// if event.what <= osEvt; high-level events store the event ID there)
if ( event.what <= osEvt )
{
AdjustCursor( event.where, sMouseRgn );
}
// dispatch on event.what
switch( event.what )
{
case nullEvent:
DoNullEvent( &event );
break;
case mouseDown:
DoMouseDown( &event);
break;
case keyDown:
case autoKey:
DoKeyDown( &event );
break;
case updateEvt:
case activateEvt:
DoWindowEvent( &event );
break;
case diskEvt:
DoDiskEvent( &event );
break;
case osEvt:
DoOSEvent( &event );
break;
case kHighLevelEvent:
DoHighLevelEvent( &event );
break;
} // switch
if ( gotEvent )
{
sSleepTime = 0; // force early idle after non-idle event
}
}
OSErr GotRequiredParams( const AppleEvent *ae )
{
DescType actualType;
Size actualSize;
OSErr err;
err = AEGetAttributePtr( ae, keyMissedKeywordAttr, typeWildCard, &actualType, nil, 0, &actualSize );
return ( err == errAEDescNotFound ) ? noErr :
( err == noErr ) ? errAEParamMissed : err;
}
static pascal OSErr HandleOpenDocument( const AppleEvent *ae, AppleEvent *reply, long refCon )
{
#pragma unused ( reply, refCon )
AEDescList docList;
AEKeyword keyword;
DescType actualType;
Size actualSize;
long numberOfDocuments, i;
FSSpec fileSpec;
OSErr err;
docList.descriptorType = typeNull;
docList.dataHandle = nil;
// extract direct parameter from the Apple event
if ( ( err = AEGetParamDesc( ae, keyDirectObject, typeAEList, &docList ) ) != noErr )
goto cleanup;
// perform the recommended check for additional required parameters
if ( ( err = GotRequiredParams( ae ) ) != noErr )
goto cleanup;
// count the items in the list of aliases
if ( ( err = AECountItems( &docList, &numberOfDocuments ) ) != noErr )
goto cleanup;
for ( i = 1; i <= numberOfDocuments; i++ )
{
// coerce the nth alias to a file system specification record
if ( ( err = AEGetNthPtr( &docList, i, typeFSS, &keyword, &actualType,
&fileSpec, sizeof( fileSpec ), &actualSize ) ) != noErr )
goto cleanup;
// open the specified file
if ( ( err = CreateWindow( &fileSpec ) ) != noErr )
goto cleanup;
}
cleanup:
AEDisposeDesc( &docList );
return err;
}
static pascal OSErr HandleOpenApplication( const AppleEvent *ae, AppleEvent *reply, long refCon )
{
#pragma unused ( reply, refCon )
OSErr err;
// perform the recommended check for additional required parameters
if ( ( err = GotRequiredParams( ae ) ) != noErr )
goto cleanup;
// create a new window from scratch
err = CreateWindow( nil );
cleanup:
return err;
}
static pascal OSErr HandleQuitApplication( const AppleEvent *ae, AppleEvent *reply, long refCon )
{
#pragma unused (reply, refCon)
AEKeyword optKey;
DescType actualType;
Size actualSize;
SavingOption saving;
OSErr err;
// default saving option is savingAsk;
saving = savingAsk;
// extract optional save options
if ( ( err = AEGetParamPtr( ae, keyAESaveOptions, typeEnumerated, &actualType, &optKey, sizeof( optKey ), &actualSize ) ) == noErr )
{
if ( optKey == kAEYes )
{
saving = savingYes;
}
else if (optKey == kAENo )
{
saving = savingNo;
}
else if ( optKey != kAEAsk )
{
err = paramErr; // for want of a better code
goto cleanup;
}
}
// perform the recommended check for additional required parameters
if ( ( err = GotRequiredParams( ae ) ) != noErr )
goto cleanup;
// actually do the quit stuff
err = DoQuit( saving );
cleanup:
return err;
}
OSErr InitializeEvents( void )
{
OSErr err;
// allocate space for the mouse region
sMouseRgn = NewRgn( );
// install AppleEvent handlers for the Required Suite
if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc( HandleOpenApplication ), 0, false ) ) != noErr )
goto cleanup;
if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc( HandleOpenDocument ), kDoOpen, false ) ) != noErr )
goto cleanup;
if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc( HandleOpenDocument ), kDoPrint, false ) ) != noErr )
goto cleanup;
if ( ( err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc( HandleQuitApplication ), 0, false ) ) != noErr )
goto cleanup;
// install Apple event handlers for a subset of the Core suite
if ( ( err = InstallCoreHandlers( ) ) != noErr )
goto cleanup;
// install Apple event handlers for inline input
if ( ( err = WEInstallTSMHandlers( ) ) != noErr )
goto cleanup;
cleanup:
return err;
}